package com.metaera.app.ui.adapters;

import android.content.Context;
import android.content.res.Resources;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lxj.xpopup.XPopup;
import com.metaera.app.LocalConfigStore;
import com.metaera.app.R;
import com.metaera.app.controller.presenter.LikeInformationPresenter;
import com.metaera.app.controller.view.BasicNetworkRequestView;
import com.metaera.app.data.model.ArticleListResponse;
import com.metaera.app.ui.activities.FlashNewsDetailsActivity;
import com.metaera.app.ui.popup.ShareFlashNewsPopup;
import com.metaera.app.utils.DateUtils;
import com.metaera.app.utils.LoginItemChildClickListener;
import com.metaera.app.utils.image.GlideApp;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;
import pub.devrel.easypermissions.EasyPermissions;

public class FlashNewsItemAdapter extends BaseMultiItemQuickAdapter<ArticleListResponse, BaseViewHolder> {

    public FlashNewsItemAdapter() {
        addItemType(ArticleListResponse.CONTENT_TYPE, R.layout.item_flash_news);
        addItemType(ArticleListResponse.DATE_TYPE, R.layout.item_flash_news_date);

        addChildClickViewIds(R.id.tv_share, R.id.tv_like_number, R.id.tv_deny_number);

        setOnItemClickListener((adapter, view, position) -> {
            Context context = view.getContext();
            ArticleListResponse item = getItem(position);
            context.startActivity(FlashNewsDetailsActivity.newInstance(context, item.getId(),
                    item.getTitle(), item.getContent(), item.getRelease_time_stamp(), item.getScore()));
        });

        setOnItemChildClickListener(new LoginItemChildClickListener((adapter, v, position) -> {

            if (v.getId() == R.id.tv_share) { // 分享快讯
                new XPopup.Builder(getContext())
                        .isLightStatusBar(false)
                        .asCustom(new ShareFlashNewsPopup(v.getContext(), getItem(position)))
                        .show();

            } else if (v.getId() == R.id.tv_like_number) { // 利好
                ArticleListResponse item = getItem(position);
                LikeInformationPresenter presenter = new LikeInformationPresenter();

                presenter.setModelAndView(new BasicNetworkRequestView<>(data -> {
                    item.setIs_like(item.getIs_like() == 1 ? 0 : 1); // 更新是否利好状态

                    // 增加利好数量
                    int currentLikeNum = item.getLike_num() + (item.getIs_like() == 1 ? 1 : -1);
                    item.setLike_num(Math.max(currentLikeNum, 0));
                    item.setDeny_num(item.getDeny_num() > 0 ? item.getDeny_num() - 1 : 0); // 增少利空数量

                    notifyItemChanged(position);
                }));

                presenter.like(item.getId(), item.getIs_like() == 0);

            } else if (v.getId() == R.id.tv_deny_number) { // 利空
                ArticleListResponse item = getItem(position);
                LikeInformationPresenter presenter = new LikeInformationPresenter();

                presenter.setModelAndView(new BasicNetworkRequestView<>(data -> {
                    item.setIs_deny(item.getIs_deny() == 0 ? 1 : 0); // 更新是否利好状态

                    int currentLikeNum = item.getDeny_num() + (item.getIs_deny() == 1 ? 1 : -1);
                    item.setDeny_num(Math.max(currentLikeNum, 0));
                    item.setLike_num(item.getLike_num() > 0 ? item.getLike_num() - 1 : 0); // 增少利空数量

//                    item.setDeny_num(item.getDeny_num() + item.getIs_deny() == 1 ? 1 : -1);
//                    item.setLike_num(item.getLike_num() - 1); // 增少利空数量

                    notifyItemChanged(position);
                }));

                presenter.deny(item.getId(), item.getIs_deny() == 0);
            }
        }, R.id.tv_share));

    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, ArticleListResponse response) {
        int timestamp = response.getRelease_time_stamp();

        if (response.getItemType() == ArticleListResponse.DATE_TYPE) {
            holder.setText(R.id.tv_date, DateUtils.getTime(timestamp, DateUtils.PATTERN_6))
                    .setVisible(R.id.tv_date_day, !DateUtils.siToday(timestamp))
                    .setText(R.id.tv_date_week, DateUtils.getWeekOfTimestamp(timestamp,
                            getContext().getResources().getStringArray(R.array.weeks)));
            return;
        }

        boolean onlyFlashTitle = LocalConfigStore.getInstance().isOnlyFlashTitle();

        ImageView ivCover = holder.getView(R.id.iv_cover);
        AppCompatTextView tvLikeNumber = holder.getView(R.id.tv_like_number);
        AppCompatTextView tvDenyNumber = holder.getView(R.id.tv_deny_number);
        Resources resources = ivCover.getContext().getResources();

        tvLikeNumber.setSelected(response.getLike_num() > response.getDeny_num());
        tvDenyNumber.setSelected(response.getDeny_num() > response.getLike_num());

        holder.setText(R.id.tv_title, response.getTitle())
                .setVisible(R.id.iv_cover, !TextUtils.isEmpty(response.getPic()))
                .setText(R.id.tv_like_number, resources.getString(R.string.item_li_hao) + " " + response.getLike_num()) // 利好
                .setText(R.id.tv_deny_number, resources.getString(R.string.item_li_kong) + " " + response.getDeny_num()) // 利空
                .setText(R.id.tv_des, response.getContent())
                .setText(R.id.tv_date, DateUtils.getTime(timestamp, DateUtils.PATTERN_4))
                .setGone(R.id.iv_cover, onlyFlashTitle)
                .setGone(R.id.tv_des, onlyFlashTitle);

        GlideApp.with(ivCover).load(response.getPic()).miniThumb().into(ivCover);

    }




}
